﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;

namespace park.System_Manager
{
    public partial class AuthorityAgency : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();

                if (Request.QueryString["type"] == "1")
                {
                    BindAuthorityList();// 绑定权限列表
                    BindOrganizationAuthority(); // 加载所选择的部门权限信息
                }
            }
        }

        private void BindTree()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            //string json = sl.SearchAgency(-1,-1,"");
            string json = "{\"data\":["
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":-1,\"id\":2,\"code\":\"YHX3678\",\"name\":\"硚口设备总站\",\" hasAutority\":1,\"order\":1024},"
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":2,\"id\":3,\"code\":\"YHX3679\",\"name\":\"硚口设备分站1\",\" hasAutority\":1,\"order\":1024},"
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":3,\"id\":10,\"code\":\"YHX3679-1\",\"name\":\"硚口设备分站1设备处1\",\"hasAutority\":1,\"order\":1024},"
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":2,\"id\":5,\"code\":\"YHX367a\",\"name\":\"硚口设备分站2\",\" hasAutority\":1,\"order\":2048},"
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":-1,\"id\":4,\"code\":\"YHX3678\",\"name\":\"硚口设备总站2\",\" hasAutority\":1,\"order\":1024},"
                + "{\"cName\":\"汉口\",\"rName\":\"硚口\",\"pid\":4,\"id\":13,\"code\":\"YHX3678\",\"name\":\"硚口设备总站3\",\" hasAutority\":1,\"order\":1024}"
                +"]}";

            JObject obj = JObject.Parse(json);

            var s = from p in obj["data"].Where(t => { return t["pid"].ToString() == "-1"; })
                    select p;

            foreach (var item in s)
            {
                TreeNode tr = new TreeNode(item["name"].ToString(),item["id"].ToString());
                tr.NavigateUrl = "?type=1&id=" + item["id"].ToString();
                this.tree_Agency.Nodes.Add(tr);
                AddTreeNode(tr,item["id"].ToString(),obj);
            }            
        }

        private void AddTreeNode(TreeNode currentNode, string pid,JObject obj)
        {
            var s = from p in obj["data"].Where(t => { return t["pid"].ToString() == pid; })
                    select p;
            foreach (var item in s)
            {
                TreeNode tr = new TreeNode(item["name"].ToString(), item["id"].ToString());
                tr.NavigateUrl = "?type=1&id=" + item["id"].ToString();
                currentNode.ChildNodes.Add(tr);
                AddTreeNode(tr, item["id"].ToString(), obj);
            }
        }

        private void BindAuthorityList()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            string json = string.Empty;

            // json = sl.GetAuthorityItems();

            json = "{\"AuthorityItems\":[{\"id\":\"1\",\"name\":\"权限1\"},{\"id\":\"2\",\"name\":\"权限2\"},{\"id\":\"3\",\"name\":\"权限3\"},{\"id\":\"4\",\"name\":\"权限4\"},{\"id\":\"5\",\"name\":\"权限5\"},{\"id\":\"6\",\"name\":\"权限6\"},{\"id\":\"7\",\"name\":\"权限7\"},{\"id\":\"8\",\"name\":\"权限8\"},{\"id\":\"9\",\"name\":\"权限9\"},{\"id\":\"10\",\"name\":\"权限10\"},{\"id\":\"11\",\"name\":\"权限11\"},{\"id\":\"12\",\"name\":\"权限12\"},{\"id\":\"13\",\"name\":\"权限13\"},{\"id\":\"14\",\"name\":\"权限14\"},{\"id\":\"15\",\"name\":\"权限15\"},{\"id\":\"16\",\"name\":\"权限16\"},{\"id\":\"17\",\"name\":\"权限17\"},{\"id\":\"18\",\"name\":\"权限18\"},{\"id\":\"19\",\"name\":\"权限19\"},{\"id\":\"20\",\"name\":\"权限20\"},{\"id\":\"21\",\"name\":\"权限21\"},{\"id\":\"22\",\"name\":\"权限22\"},{\"id\":\"23\",\"name\":\"权限23\"}]}";

            JObject obj = JObject.Parse(json);

            foreach (var item in obj["AuthorityItems"])
            {
                ListItem li = new ListItem(item["name"].ToString(), item["id"].ToString());
                this.cblAuthority.Items.Add(li);
            }

        }

        private void BindOrganizationAuthority()
        {
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

                // string json = sl.GetOrganizationAuthority(id);

                string json = "{\"OrganizationID\":1,\"Name\":\"硚口设备总站\",\"AuthorityItems\":\"1,3,4,5,6\"}";
                JObject obj = JObject.Parse(json);
                this.ltName.Text = obj["Name"].ToString() + "的权限";

                string authority = obj["AuthorityItems"].ToString();

                string[] arr = authority.Split(',');

                foreach (var item in arr)
                {
                    ListItem li = cblAuthority.Items.FindByValue(item);
                    if (li != null)
                    {
                        li.Selected = true;
                    }
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];

            if (!string.IsNullOrEmpty(id))
            {
                string name = ltName.Text.Replace("的权限", "");

                string authority = string.Empty;
                foreach (ListItem item in this.cblAuthority.Items)
                {
                    if (item.Selected)
                    {
                        authority += item.Value + ",";
                    }
                }

                string json = "{" + string.Format("\"OrganizationID\":{0},\"Name\":\"{1}\",\"AuthorityItems\":\"{2}\"", id, name, authority.Trim(',')) + "}";

                cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

                // bool flag = sl.UpdateOrganizationAuthority(json);
                bool flag = true;

                string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('您还没有选择任何机构，请先选择要修改的机构。');</script>");
            }
        }
    }
}